package graphics;



import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;

import system.ActionPoint;
import system.Board;
import system.Edge;
import system.Game;
import system.HouseType;
import system.Player;
import system.Tile;
import system.Tiles;




public class Gui extends JPanel {


	private static final long serialVersionUID = 1L;
	BufferedImage water;
	BufferedImage wood;
	BufferedImage sheep;
	BufferedImage clay;
	BufferedImage grain;
	BufferedImage erts;
	
	BufferedImage frame;
	BufferedImage field;
	
	BufferedImage[] hexes = new BufferedImage[6];
	BufferedImage[] nrs = new BufferedImage[13];
	BufferedImage[] houses = new BufferedImage[5];
	Color[] colors = new Color[5];
	
	private final int TILEWIDTH = 125;//150;
	private final int TILEHEIGHT = 75;//100;
	private final int FPS = 30;
	Board b;
	Game g;
	ArrayList<Tile> tiles; 
	int possibleMoves = 0;
	int tellertje =0;
	ActionPoint p1;
	
		public Gui(){
			
			

			loadImages();
			
			g= new Game();
			b = g.getBoard();
			setMinimumSize(new Dimension(800,600));
			this.setMaximumSize(new Dimension(1900,1080));
			this.setPreferredSize(new Dimension(1280,1024));
			this.setSize(getPreferredSize());
		
			repaint();
			
			this.addMouseListener(new MouseListener(){

				@Override
				public void mouseClicked(MouseEvent click) {
					double factorx = getSize().getWidth()/1280;
					double factory = getSize().getHeight()/800;
					double x = (click.getX()/factorx/TILEWIDTH);
					double y = (click.getY()/factory/TILEHEIGHT);
					//1280/800
					if( click.getButton() == MouseEvent.BUTTON2)
					{
						ActionPoint point = b.getPointAt(x, y);
						if (point!=null){
							g.addHouse(point, g.getPlayers().get(0));
							drawMAp();
							repaint();
							
						}
						
					}
					
					if( click.getButton() == MouseEvent.BUTTON1)
					{
						System.out.println(g.getPlayers().get(0).getActionPoints().size());
						//System.out.println("(" + x + ',' + y + ")");
						ActionPoint point = b.getPointAt(x, y);
						if (point!=null){
//							System.out.println(point);
//							g.addHouse(point, g.getPlayers().get(0));
							
							p1 = point;
							System.out.println(g.getBoard().getNeighbours(point));
							System.out.println(point.getOwner());
//							drawMAp();
//							repaint();
							//System.out.println(point.getTiles());
							
						}
							
						
						
					}
					if( click.getButton() == MouseEvent.BUTTON3)
					{
						ActionPoint point = b.getPointAt(x, y);
						if (point!=null){
							System.out.println(point);
							if (p1 != null){
								Edge e = g.getBoard().getEdge(new Edge(p1,point));
							if (e != null){
								if (g.addEdge(e, g.getPlayers().get(0))){
								drawMAp();
							repaint();
							p1 = point;	
								}
							
							}
							}
						}
					}
						
					
					
					
				}

				@Override
				public void mouseEntered(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseExited(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mousePressed(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseReleased(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}});
			
			
			
			
			
			Timer t = new Timer(1500, new ActionListener(){



				//timer to set when to draw the screen and update values
				@Override
				public void actionPerformed(ActionEvent arg0) {
				
						
					
				}

			});
			
			
			
			t.start();
			drawMAp();
			repaint();
			KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
			manager.addKeyEventDispatcher(new MyDispatcher());
			ComponentFrame.showFrame("Katan", this);
			//possibleMoves = game.getPossibleMoves(game.getCurrent(), game.getDie()).size();

		}



		
		
		private void loadImages(){
			try {
			
				water  =  ImageIO.read(getClass().getResource("/hex.png"));
				wood = ImageIO.read(getClass().getResource("/wood.png"));
				erts = ImageIO.read(getClass().getResource("/erts.png"));
				grain = ImageIO.read(getClass().getResource("/grain.png"));
				sheep = ImageIO.read(getClass().getResource("/sheep.png"));
				clay = ImageIO.read(getClass().getResource("/clay.png"));
					
				hexes[0] = water;
				hexes[1] = wood;
				hexes[2] = erts;
				hexes[3] = grain;
				hexes[4] = sheep;
				hexes[5] = clay;
				for (int i = 0; i < nrs.length; i++) {
					nrs[i] = ImageIO.read(getClass().getResource("/numbers.png")).getSubimage(i*25, 0, 25, 25);
				}
				
				for (int i = 0; i < houses.length; i++) {
					houses[i] = ImageIO.read(getClass().getResource("/houses.png")).getSubimage(i*64, 0, 64, 64);
					
				}
				
				colors[0]= new Color(217,1,16);
				colors[1]= new Color(28,75,243);
				colors[2]= new Color(13,219,56);
				colors[3]= new Color(248,222,0);
				colors[4]= new Color(150,30,242);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		
		
		@Override
		public void paint(Graphics g){
			
			
			
			g.drawImage(field, 0, 0, this.getWidth(), this.getHeight(), this);
		}





		private void drawMAp() {
			
			field = new BufferedImage(1280,800, BufferedImage.TYPE_INT_ARGB);
			Graphics2D gg = (Graphics2D) field.createGraphics();
			gg.setStroke(new BasicStroke(4));
			gg.setColor(new Color(75, 190, 180));
			gg.fillRect(0, 0, 1280, 800);
			Tile[][] tiles = b.getBord();
			gg.setColor(Color.BLACK);
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 11; j++) {
				
				Tile tile = tiles[j][i];
				if (tile.getTile() != null){
				gg.drawImage(hexes[tile.getTile().ordinal()], (int)((tile.getX()-0.5)*TILEWIDTH), (int)((tile.getY()-0.5)*TILEHEIGHT ),TILEWIDTH,TILEHEIGHT, null);
				
				
				if (!tile.getTile().equals(Tiles.WATER))
					gg.drawImage(nrs[tile.getNr()],(int)((tile.getX()-0.5)*TILEWIDTH+(0.4*TILEWIDTH)),(int)((tile.getY()-0.5)*TILEHEIGHT +(0.30*TILEHEIGHT)), (int)(TILEWIDTH *0.2),(int)(TILEWIDTH *0.2),null);
				}
			}
		}
		
		
		for (Edge e: b.getEdges()){
			if(e.getOwner()!= null){
//				tellertje++;
//				System.out.println(tellertje);
				gg.setColor(colors[e.getOwner().getColor().ordinal()]);
			gg.drawLine((int)(e.getP1().getX()*TILEWIDTH), (int)(e.getP1().getY()*TILEHEIGHT), (int)(e.getP2().getX()*TILEWIDTH),  (int)(e.getP2().getY()*TILEHEIGHT));
			}
		}
		for (Player player : g.getPlayers()) {
			
		
		for (ActionPoint p : player.getActionPoints()) {
			if (p.getType()==HouseType.HOUSE)
			{
				gg.drawImage(houses[player.getColor().ordinal()],   (int)((p.getX()*TILEWIDTH)-(0.3*TILEWIDTH)), (int)((p.getY()*TILEHEIGHT-(0.3*TILEHEIGHT))), (int)(0.6*TILEWIDTH),(int)(0.6*TILEHEIGHT),null);
			}
		}
		}
		
		for (ActionPoint p : b.getPoints()) {
			gg.setColor(Color.BLACK);
			gg.drawString(p.toString(),(int)((p.getX()*TILEWIDTH)-(0.025*TILEWIDTH)), (int)((p.getY()*TILEHEIGHT-(0.05*TILEHEIGHT))));
		//	gg.fillOval((int)((p.getX()*TILEWIDTH)-(0.05*TILEWIDTH)), (int)((p.getY()*TILEHEIGHT-(0.05*TILEHEIGHT))), (int)(0.1*TILEWIDTH),(int)(0.1*TILEHEIGHT));
		}
		System.out.println(b.getEdges().size());
		
			
			gg.dispose();
		}





		
		
		@Override
		public void update(Graphics g){
			paint(g);
		}
	
	

		public static BufferedImage rotate(BufferedImage img, int angle) {
			angle = -angle;
			int w = img.getWidth();
			int h = img.getHeight();
			BufferedImage dimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
			Graphics2D g = dimg.createGraphics();
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
			g.rotate(Math.toRadians(angle), w / 2, h / 2);

			g.drawImage(img, null, 0, 0);
			return dimg;
		}
		
		
		public static void main(String args[]){
		
			Gui g = new Gui();
		}
		
		private class MyDispatcher implements KeyEventDispatcher {
			@Override
			public boolean dispatchKeyEvent(KeyEvent e) {

				 if (e.getID() == KeyEvent.KEY_RELEASED) {

					int keyCode = e.getKeyCode();
					//switch (keyCode) {
					if (keyCode == KeyEvent.VK_SPACE)
						{drawMAp();
						repaint();
						}
					
					//}
				}
				return false;
			}

		
		}
}
